# Echo Server
# 
# @see http://pymotw.com/2/socket/tcp.html
# 
# This sample program, based on the one in the standard library documentation, 
# receives incoming messages and echos them back to the sender. 
# It starts by creating a TCP/IP socket.

import socket
import sys

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Then bind() is used to associate the socket with the server address. 
# In this case, the address is localhost, referring to the current server, 
# and the port number is 10000.

# Bind the socket to the port
server_address = ('localhost', 3334)
print('starting up on %s port %s', server_address, file=sys.stderr)
sock.bind(server_address)

# Calling listen() puts the socket into server mode, and accept() waits for an incoming connection.

# Listen for incoming connections
sock.listen(1)

while True:
    # Wait for a connection
    print('waiting for a connection', file=sys.stderr)
    connection, client_address = sock.accept()

# accept() returns an open connection between the server and client, 
# along with the address of the client. The connection is actually a different socket 
# on another port (assigned by the kernel). 
# Data is read from the connection with recv() and transmitted with sendall().

    try:
        print('connection from', client_address, file=sys.stderr)

        # Receive the data in small chunks and retransmit it
        while True:
            data = connection.recv(256)
 
            print('received', data, file=sys.stderr)
            if data:
                print('sending data back to the client', file=sys.stderr)
                connection.sendall(data)
            else:
                print('no more data from', client_address, file=sys.stderr)
                break            
    finally:
        # Clean up the connection
        connection.close()
